package com.ty.web;

import com.ty.bean.Menu;
import com.ty.bean.Role;
import com.ty.bean.Users;
import com.ty.service.MenuService;
import com.ty.service.RoleService;
import com.ty.service.UsersService;
import com.ty.service.impl.MenuServiceImpl;
import com.ty.service.impl.RoleServiceImpl;
import com.ty.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 田野
 */
@WebServlet(urlPatterns = {"/power/role/roles"})
public class RoleServlet extends HttpServlet {
    private RoleService roleService = new RoleServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("select".equals(method)){
            select(req, resp);
        }else if ("selectmenus".equals(method)){
            selectmenus(req, resp);
        }else if ("insert".equals(method)){
            insert(req, resp);
        }
    }
    // 新增角色
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、接收参数
        String roleName = req.getParameter("roleName");
        String[] menuIds = req.getParameterValues("menuId");
        String state = req.getParameter("state");
        // 2、调用service方法
        int i = roleService.insertRole(roleName, state, menuIds);
        if (i>0){
            // 3、存值并进行页面跳转
            resp.sendRedirect("/power/role/roles?method=select");
        }else {
            // 由 "/power/role/roles?method=selectmenus" 跳转到新增页面add.jsp
            resp.sendRedirect("/power/role/roles?method=selectmenus");
        }


    }


    // 查询菜单列表
    protected void selectmenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、接收参数
        // 2、调用service方法
        MenuService menuService = new MenuServiceImpl();
        List<Menu> menuList = menuService.getMenuList();
        // 3、存值并进行页面跳转
        req.setAttribute("menuList", menuList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);

    }



    // 查询数据（分页）
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、接收参数(分页参数和模糊查询参数)
        String index = req.getParameter("index");
        int pageIndex = (index==null || index.length()==0)?1:Integer.parseInt(index);

        // 2、调用service层方法(需要调用两个方法：1、查询数据列表的方法 2、查询总条数的方法)
        List<Role> roleList = roleService.getRoleList(pageIndex, 5);
        int total = roleService.total();
        int totalPages = total%5 > 0 ? (total/5+1):total/5;

        // 3、存值并跳转页面
        req.setAttribute("roleList", roleList);
        req.setAttribute("pageIndex", pageIndex);
        req.setAttribute("totalPages", totalPages);
        req.setAttribute("total", total);
        req.setAttribute("size", 5);

        // 不能再"list.jsp" 前面加 / ，否则会从根目录下去找这个页面
        req.getRequestDispatcher("list.jsp").forward(req, resp);

    }


}
